package demo04.controller;

import com.sun.javafx.scene.input.InputEventUtils;
import com.tedu.InputUtil;
import demo04.mapper.IUserMapper;
import demo04.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component
public class UserController {
@Autowired
private IUserMapper userMapper;
    public void start() {

        System.out.println("开始工作");
        System.out.println("请选择对应的操作");
        System.out.println("1.查询用户信息,2登录，3，修改用户信息,4:用户列表");
        Scanner scanner = new Scanner(System.in);
        int num = Integer.parseInt( scanner.nextLine()) ;
        switch (num){
            case 1:
                this.regUser();
                break;
            case 2:
                this.loginUser();
                break;
            case 3:
              updateUser();
                break;
            case 4:
               listUser();
                 break;
            default:
                System.out.println("其他功能还在完善，404");
        }
    }
    public void listUser(){
        System.out.println("用户列表 :");
        List<User> userList = userMapper.selectAll();
        for (User user : userList){
            System.out.println(user);
        }
    }

    public void updateUser(){
        System.out.println("修改用户信息");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的用户的用户名:");
        String username = sc.nextLine();
        System.out.println("请输入新密码:");
        String password = sc.nextLine();
        System.out.println("请输入新昵称:");
        String nickname = sc.nextLine();
        System.out.println("请输入新年龄:");
        int age = sc.nextInt();
        User user = new User(null, username, password, nickname, age);
        int num = userMapper.update(user);
        System.out.println(num > 0 ? "修改成功" : "修改失败");

    }

    public void regUser() {
        System.out.println("开始处理用户注册");
        System.out.println("请输入用户名");
        Scanner scanner = new Scanner(System.in);
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        int age = Integer.parseInt(scanner.nextLine());

        if (userMapper.selectByUsername(username) != null) {
            System.out.println("用户名已存在");
        } else {
            User user = new User(null, username, password, nickname, age);
            int num = userMapper.insert(user);
            System.out.println(num > 0 ? "注册成功" : "注册失败");
        }
    }
    public void loginUser(){
        System.out.println("开始处理用户登录");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        User user = userMapper.selectByUsername(username);
        if (user==null){
            System.out.println("用户名不存在,输入错误");
        }else {
            if (user.getPassword().equals(password)){
                System.out.println("登录成功"+user.getNickname());
            }else {
                System.out.println("密码错误");
            }
        }
    }
}
